/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.entity.VBrandAgreement;
import com.ykht.proj.activity.entity.VBrandAgreementRenewal;
import com.ykht.proj.activity.entity.VDiscountPlanShop;
import com.ykht.proj.activity.mapper.VBrandAgreementMapper;
import com.ykht.proj.activity.service.VBrandAgreementService;
import com.ykht.proj.activity.service.VDiscountPlanShopService;
import com.ykht.proj.activity.vo.VBrandAgreementVo;
import com.ykht.proj.activity.vo.VDiscountPlanVo;
import com.ykht.proj.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 品牌协议表
 *
 * @author csg
 * @date 2021-05-18 14:25:31
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class VBrandAgreementServiceImpl extends ServiceImpl<VBrandAgreementMapper, VBrandAgreement> implements VBrandAgreementService {

	private final VBrandAgreementMapper vBrandAgreementMapper;
	private final VDiscountPlanShopService vDiscountPlanShopService;
	/**
	 * 把 品牌协议表 的数据 copy 到  品牌协议续约表
	 * @param vBrandAgreement 品牌协议表
	 * @return VBrandAgreementRenewal  品牌协议续约表
	 */
	private VBrandAgreementRenewal copyVBrandAgreementRenewal(VBrandAgreement vBrandAgreement){
		VBrandAgreementRenewal vBrandAgreementRenewal = new VBrandAgreementRenewal();
		vBrandAgreementRenewal.setAgreementId(vBrandAgreement.getId());
		vBrandAgreementRenewal.setAgreementId(vBrandAgreement.getId());
		vBrandAgreementRenewal.setCompanyId(vBrandAgreement.getCompanyId());
		vBrandAgreementRenewal.setBrandId(vBrandAgreement.getBrandId());
		vBrandAgreementRenewal.setAgreRenewalCode(vBrandAgreement.getAgreCode());
		vBrandAgreementRenewal.setStaTime(vBrandAgreement.getStaTime());
		vBrandAgreementRenewal.setEndTime(vBrandAgreement.getEndTime());
		vBrandAgreementRenewal.setSuspendReason(vBrandAgreement.getSuspendReason());
		vBrandAgreementRenewal.setOperationUser(SecurityUtils.getUserId());
		vBrandAgreementRenewal.setOperationRole(SecurityUtils.getUser().getUserType());
		vBrandAgreementRenewal.setCreateUser(SecurityUtils.getUserId());
		vBrandAgreementRenewal.setCreateTime(LocalDateTime.now());
		return vBrandAgreementRenewal;
	}

	/**
	 * 新增品牌协议表 并且给 品牌协议续约表 添加记录
	 *
	 * @param vBrandAgreement
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveAndRenewal(VBrandAgreement vBrandAgreement) {
		vBrandAgreement.setCreateUser(SecurityUtils.getUserId());
		vBrandAgreement.setCreateTime(LocalDateTime.now());
		this.save(vBrandAgreement);
		VBrandAgreementRenewal vBrandAgreementRenewal = copyVBrandAgreementRenewal(vBrandAgreement);
		boolean insert = vBrandAgreementRenewal.insert();
	}

	/**
	 * 续约品牌协议表 并且给 品牌协议续约表 添加记录
	 *
	 * @param vBrandAgreementVo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateAndRenewal(VBrandAgreementVo vBrandAgreementVo) {
		VBrandAgreement agreement = this.getById(vBrandAgreementVo.getId());
		VBrandAgreement vBrandAgreement = BeanUtil.toBean(vBrandAgreementVo, VBrandAgreement.class);
		vBrandAgreement.setUpdateUser(SecurityUtils.getUserId());
		vBrandAgreement.setUpdateTime(LocalDateTime.now());
		this.updateById(vBrandAgreement);
		VBrandAgreement brandAgreement = this.getById(vBrandAgreement.getId());
		VBrandAgreementRenewal vBrandAgreementRenewal = copyVBrandAgreementRenewal(brandAgreement);
		if (null == vBrandAgreementVo.getStaTime()){
			vBrandAgreementRenewal.setStaTime(agreement.getEndTime().plusSeconds(1));
		}
		boolean insert = vBrandAgreementRenewal.insert();
	}

	/**
	 * 品牌协议 分页查询
	 *
	 * @param page              分页
	 * @param vBrandAgreementVo 条件
	 * @return
	 */
	@Override
	public IPage<VBrandAgreementVo> getBrandAgreementListPage(Page page, VBrandAgreementVo vBrandAgreementVo) {
		IPage<VBrandAgreementVo> listPage = vBrandAgreementMapper.getBrandAgreementListPage(page, vBrandAgreementVo);
		if (null != listPage.getRecords() && listPage.getRecords().size() > 0){
			List<Long> brandIdList = listPage.getRecords().stream().map(VBrandAgreementVo::getBrandId).collect(Collectors.toList());
			List<VDiscountPlanVo> shopCount = vDiscountPlanShopService.getShopCount(brandIdList);
			if (null != shopCount && shopCount.size() > 0 && null != shopCount.get(0)){
				listPage.getRecords().stream().forEach(itme ->{
					shopCount.stream().forEach(shop ->{
						if (itme.getBrandId().equals(shop.getBrandId())){
							itme.setShopCount(shop.getShopCount());
						}
					});
				});
			}
		}
		return listPage;
	}

	/**
	 * 通过id查询品牌协议表详情
	 *
	 * @param id
	 * @return
	 */
	@Override
	public VBrandAgreementVo getByIdBrandAgreement(Long id) {
		return vBrandAgreementMapper.getByIdBrandAgreement(id);
	}
}
